// src/main/java/com/bookmanager/controller/BookDetailController.java
package com.bookmanager.controller;

import com.bookmanager.model.Book;
import com.bookmanager.model.User;
import com.bookmanager.service.BookService;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;
import java.io.IOException;

@WebServlet("/book-detail")
public class BookDetailController extends HttpServlet {
    private BookService bookService = new BookService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");

        if (user == null) {
            response.sendRedirect("login.jsp");
            return;
        }

        try {
            int bookId = Integer.parseInt(request.getParameter("bookId"));
            Book book = bookService.getBookById(bookId);

            if (book != null) {
                request.setAttribute("book", book);
                request.getRequestDispatcher("book-detail.jsp").forward(request, response);
            } else {
                response.sendRedirect("books?message=book_not_found");
            }
        } catch (NumberFormatException e) {
            response.sendRedirect("books?message=invalid_book_id");
        }
    }
}